#include<iostream>
#include<unistd.h>
#include<sys/wait.h>
#include <stdlib.h>
#include <sys/types.h>
using namespace std;

//int main()
//{
//  
//  printf("替换前我能看见\n");
//  
//  int ret=execl("/usr/bin/ls","ls","-l",NULL);
//
//  if(ret==-1) printf("没有替换成功\n");
//  
//  return 0;
//}

int main()
{
 
   pid_t ret= fork();
  
  //char* const argv[]={"ls","-a","-l",NULL};
  char* const argv[]={"./other",NULL};

  char* const envp[]={"start=100","end=200",NULL};
  
   if(ret==0)
   {
   printf("我是子进程我被替换了ls\n");

   //int t=execv("/usr/bin/ls",argv);
  // int t=execvp("ls",argv);

   //int t=execlp("ls","ls","-l",NULL);
   

  // int t=execl("./other","./other",NULL);
  
  //int t=execle("./other","./other",NULL,envp);
    
    //int t=execve("./other",argv,envp);

    int t=execvpe("./other",argv,envp);

   if(t==-1) printf("子进程替换失败\n"); 
 
   exit(-1);
  }
  else
  {
     int status=0;

     waitpid(-1,&status,0);
   
     if(WIFEXITED(status))
     {
       printf("子进程被替换的良好  exit_code:%d\n",WEXITSTATUS(status)); 
     }
     else
     {
       printf("子进程替换失败,code %d\n",WEXITSTATUS(status));
     }

  }

  return 0;
}

